/**
 * 
 */
package paymentController;



import java.net.URL;
import java.util.ResourceBundle;

import paymentLogic.PaymentLogic;
import validator.Validator;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;

/**
 * @author Yokesh Kumar
 * This class is loaded when payment FXML is loaded
 */
public class PaymentController implements Initializable{

	@FXML
	private TextField creditCardNo1;
	@FXML
	private TextField creditCardNo2;
	@FXML
	private TextField creditCardNo3;
	@FXML
	private TextField creditCardNo4;
	@FXML
	private TextField expiryMonth;
	@FXML
	private TextField expiryYear;
	@FXML
	private PasswordField cvv;
	@FXML
	private TextField amount;
	@FXML
	private ChoiceBox<String> cardType;
	@FXML
	private String paymentType;
	@FXML
	private TextField givenAmount;
	@FXML
	private Label message;
	@FXML
	private Button membershipPay;
	
	

	@FXML
	public void makeMembershipPayment(ActionEvent event){
		System.out.println("make payment invoked");
		membershipPay.setDisable(true);
		PaymentLogic paymentLogic = new PaymentLogic();
		// checkCreditcard()
		// checkexpiryDate()
		// based on the payment type load methods
		String msg = paymentLogic.makeCreditcardPaymentForMembership(creditCardNo1,creditCardNo2,creditCardNo3,creditCardNo4,expiryMonth,expiryYear,cardType);
		if(msg != null && !msg.isEmpty() && msg.contains("successfull")){
			message.setTextFill(Color.GREEN);
			message.setText(msg);
		}
		else if(msg != null && !msg.isEmpty()){
			System.out.println("setting disable");
			message.setText(msg);
			membershipPay.setDisable(false);
		}
		else{
			message.setText("Payment failed");
			membershipPay.setDisable(false);
		}
		
	}

	@FXML
	public void makeMembershipRenewPayment(ActionEvent event){
		System.out.println("make Renew payment invoked");
		membershipPay.setDisable(true);
		PaymentLogic paymentLogic = new PaymentLogic();
		// checkCreditcard()
		// checkexpiryDate()
		// based on the payment type load methods
		String msg = paymentLogic.makeCreditcardPaymentForMembershipRenewal(creditCardNo1,creditCardNo2,creditCardNo3,creditCardNo4,expiryMonth,expiryYear,cardType);
		if(msg != null && !msg.isEmpty() && msg.contains("successfull")){
			message.setTextFill(Color.GREEN);
			message.setText(msg);
		}
		else if(msg != null && !msg.isEmpty()){
			System.out.println("setting disable");
			message.setText(msg);
			membershipPay.setDisable(false);
		}
		else{
			message.setText("Payment failed");
			membershipPay.setDisable(false);
		}
		
	}

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		Validator.addTextLimiter(creditCardNo1, 4);
		Validator.addTextLimiter(creditCardNo2, 4);
		Validator.addTextLimiter(creditCardNo3, 4);
		Validator.addTextLimiter(creditCardNo4, 4);
		Validator.addTextLimiter(expiryMonth, 2);
		Validator.addTextLimiter(expiryYear, 2);
	}

}
